import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter3demo/%E8%8E%B7%E5%8F%96%E5%B8%83%E5%B1%80/sized_notification.dart';
import 'dart:ui' as ui;

import 'package:flutter3demo/CustomRootElement.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Material(child: TestPage()),
    );
  }
}

class TestPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return TestPageState();
  }
}

class TestPageState extends State<TestPage> {
  List<String> tempList = [];
  int aC = 0;
  List data = [1, 2, 3, 4];

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return _body();
  }

  /*
  * @override
  void mount(Element? parent, Object? newSlot) {
    super.mount(parent, newSlot);
    final MultiChildRenderObjectWidget multiChildRenderObjectWidget = widget as MultiChildRenderObjectWidget;
    final List<Element> children = List<Element>.filled(multiChildRenderObjectWidget.children.length, _NullElement.instance);
    Element? previousChild;
    for (int i = 0; i < children.length; i += 1) {
      final Element newChild = inflateWidget(multiChildRenderObjectWidget.children[i], IndexedSlot<Element?>(i, previousChild));
      children[i] = newChild;
      previousChild = newChild;
    }
    _children = children;
  }*/
  Widget _body() {
    String textStr = "阿达可视电话阿克苏记得哈a" * 5;
    TextStyle style = TextStyle(
      color: Colors.white,
      fontSize: 14,
    );

    double maxWith = 200;
    return Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        key: Key(Random().nextInt(100000000).toString()),
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          GestureDetector(
            key: Key(Random().nextInt(100000000).toString()),
            onTap: () {
              RichText text = RichText(
                text: TextSpan(text: textStr, style: style),
                textAlign: TextAlign.center,
                textDirection: TextDirection.ltr,
                softWrap: true,
                overflow: TextOverflow.ellipsis,
                textScaleFactor: 1,
                maxLines: 7,
                strutStyle: StrutStyle(fontSize: 14, height: null, forceStrutHeight: true),
                textWidthBasis: TextWidthBasis.parent,
                textHeightBehavior: null,
                locale: Localizations.maybeLocaleOf(context),
                selectionColor: null,
              );

              RenderParagraph p = text.createRenderObject(context);
              p.layout(BoxConstraints(maxWidth: maxWith, maxHeight: 200, minHeight: 0, minWidth: 0));
              print("p.size=====${p.size}");

              RenderParagraph a = RenderParagraph(
                TextSpan(text: textStr, style: style),
                textAlign: TextAlign.center,
                textDirection: TextDirection.ltr,
                softWrap: true,
                overflow: TextOverflow.ellipsis,
                textScaleFactor: 1,
                maxLines: 7,
                strutStyle: StrutStyle(fontSize: 14, height: null, forceStrutHeight: true),
                textWidthBasis: TextWidthBasis.parent,
                textHeightBehavior: null,
                locale: Localizations.maybeLocaleOf(context),
                selectionColor: null,
              );

              a.layout(BoxConstraints(maxWidth: maxWith, maxHeight: 200, minHeight: 0, minWidth: 0));

              print(a.size);

              // 1:走layout
              // a.performLayout();
              // {
              //     double width = a.computeMinIntrinsicWidth(100);
              //     double height = a.computeMaxIntrinsicHeight(100);
              //
              //     if (width > maxWidth) {
              //       width = maxWidth;
              //     }
              //     print("width==$width==height==$height");
              //
              Size aa = computeTextSize(context, maxWith, textStr, style,
                  maxLines: 7, fixWidth: 0, strutStyle: StrutStyle(fontSize: 14, height: null, forceStrutHeight: true));
              print("aa==$aa");
              //   }
            },
            child: Container(
              width: 100,
              height: 100,
              color: Color.fromARGB(
                  Random().nextInt(255), Random().nextInt(255), Random().nextInt(255), Random().nextInt(255)),
            ),
          ),
          NotificationListener<SizedNotification>(
            onNotification: (SizedNotification notification) {
              print("notification.size==${notification.size}");
              return true;
            },
            child: SizedNotifier(
              child: Container(
                  constraints: BoxConstraints(
                    maxWidth: maxWith,
                  ),
                  color: Colors.red,
                  child: Text(
                    textAlign: TextAlign.center,
                    textDirection: TextDirection.ltr,
                    softWrap: true,
                    overflow: TextOverflow.ellipsis,
                    textScaleFactor: 1,
                    maxLines: 7,
                    strutStyle: StrutStyle(fontSize: 14, height: null, forceStrutHeight: true),
                    textStr,
                    style: style,
                    textWidthBasis: TextWidthBasis.parent,
                    textHeightBehavior: null,
                    locale: Localizations.maybeLocaleOf(context),
                    selectionColor: null,
                  )),
            ),
          ),
        ],
      ),
    );
  }

  /// 计算文本的size
  Size computeTextSize(context, double maxWidth, String value, TextStyle textStyle,
      {int maxLines = 1, double fixWidth = 2.0, double fixHeight = 0.0, StrutStyle? strutStyle}) {
    //很迷的bug，这行代码不要动
    value = value.replaceAll('/', '*').replaceAll('-', ',');

    TextPainter painter = TextPainter(
      ///AUTO：华为手机如果不指定locale的时候，该方法算出来的文字高度是比系统计算偏小的。
      locale: WidgetsBinding.instance.window.locale,
      maxLines: maxLines,
      textDirection: ui.TextDirection.ltr,
      //字体缩放大小
      textScaleFactor: MediaQuery.of(context).textScaleFactor,
      text: TextSpan(text: value, style: textStyle),
      strutStyle: strutStyle,
    );
    painter.layout(maxWidth: maxWidth);

    ///文字的宽度:painter.height
    return Size(painter.width + fixWidth, painter.height + fixHeight);
  }
}

// nderParagraph a = RenderParagraph(
//   TextSpan(
//       text: textStr,
//       style: style
//   ),
//   textAlign: TextAlign.center,
//   textDirection: TextDirection.ltr,
//   softWrap: true,
//   overflow: TextOverflow.clip,
//   textScaleFactor: 1,
//   textWidthBasis: TextWidthBasis.parent,
//   maxLines: 6,
// );
//
// a.layout(BoxConstraints(maxWidth: 100,maxHeight: 200,minHeight: 0,minWidth: 0));
//
// print(a.size);
